`iALK` <-
function(freq.mat, length.vec1, length.vec2, stop.value=0.001)
	{
	if(length(length.vec1) != length(length.vec2) || length(length.vec2) != nrow(freq.mat) || c(length.vec1, length.vec2, apply(freq.mat, 1, sum)) <= 0)
		{
		print(paste("Age/Length data = Length data"))
		print(paste(length(length.vec1),"=",length(length.vec2)))
		print(paste("Length data = Length in Age/Length key"))
		print(paste(length(length.vec2),"=",nrow(freq.mat)))
	    	stop("The number of length-classes must be the same in all data sets and all length-classes must have been sampled.")
		}
	nij1.temp <- length.vec1 * freq.mat/apply(freq.mat, 1, sum)
  	denom <- apply(nij1.temp, 2, sum)
	denom[denom==0] <- 1
	ialk.temp <- sweep(nij1.temp, 2, denom,"/")
	pj2.temp <- rep(1/ncol(freq.mat), ncol(freq.mat))
	criterion <- 10
	iterations <- 0
	while(criterion > stop.value)
		{
		iterations <- iterations + 1
		pj2.temp.old <- pj2.temp
		denom <- apply(sweep(ialk.temp, 2, pj2.temp, "*"),1, sum)
		denom[denom==0] <- 1
		alk.temp <- sweep(ialk.temp, 2, pj2.temp, "*")/denom
		nij2.temp <- length.vec2 * alk.temp
		pj2.temp <- apply(nij2.temp, 2, sum)/sum(nij2.temp)
		criterion <- sum(abs(pj2.temp - pj2.temp.old))
		}
	tablaIALK <<-nij2.temp
	iteraciones <<- iterations 
	#output <- list(nij2.temp, "Number of iterations to convergence" = iterations)
	return(nij2.temp)
	}
